package com.oocl.server.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.oocl.server.dao.UserDao;
import com.oocl.server.pojo.User;
import com.oocl.server.util.DBUtil;
import com.oocl.server.util.Logger;

public class UserDaoImpl implements UserDao{

	public int addUser(User u) {
		Connection con = null;
		PreparedStatement pst = null;
		String sql = "insert into users(uname, pwd, oonline) values(?, ?, ?)";
		int m = 0;
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
            pst.setString(1, u.getUserName());
            pst.setString(2, u.getPassWord());
            pst.setBoolean(3, u.isOnline());
			m = pst.executeUpdate();  //返回执行的语句数目
		} catch (Exception e) {
			Logger.log("UserDao数据插入异常");
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
		return m;
	}

	public int deleteUser(String name) {
		Connection con = null;
  		PreparedStatement pst = null;
  		String sql = "delete from users where uname = ?";
  		int m = 0;
  		try {
  			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, name);
			m = pst.executeUpdate();
		} catch (Exception e) {
			Logger.log("UserDao数据删除异常");
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
  		return m;
	}

	public int updateUser(User u) {
		Connection con = null;
		PreparedStatement pst = null;
		String sql = "update users set pwd=?, oonline = ? where uname = ?";
		int m = 0;
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, u.getPassWord());
			pst.setBoolean(2, u.isOnline());
			pst.setString(3, u.getUserName());
			
			m = pst.executeUpdate();   
		} catch (Exception e) {
			Logger.log("UserDao数据更新异常");
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
		return m;
	}

	public User searchUser(String name) {
		Connection con = null;
  		PreparedStatement pst = null;
  		ResultSet rs = null;
  		String sql = "select * from users where uname = ?";
  		User user = null;
  		try {
  			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, name);
			rs = pst.executeQuery();
			
			if(rs.next()){
				user= new User(rs.getString("uname"), rs.getString("pwd"), rs.getBoolean("oonline"));
			}
		} catch (Exception e) {
			Logger.log("UserDao数据查询异常");
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
			return user;
		}
  		
  		//return user;
	}

	public List<User> findAll() {
		List<User> list = new ArrayList<User>();
		Connection con = null;
  		PreparedStatement pst = null;
  		ResultSet rs = null;
  		String sql = "select * from users";
  		User user = null;
  		try {
  			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			
			while(rs.next()){
				user= new User(rs.getString("uname"), rs.getString("pwd"), rs.getBoolean("oonline"));
				list.add(user);
			}
		} catch (Exception e) {
			Logger.log("UserDao数据查询异常");
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		} 
  		
  		return list;
	}

}
